import {Page} from '@/base/Page'
import {deepAssign} from '@/base/util/Objects';
import {Firmware} from '@/base/model/Firmware';
import {DeviceSort} from '@/base/model/DeviceSort';
import {DeviceModel} from '@/base/model/DeviceModel';
import {
  deleteFirmwareId,
  getFirmware,
  getFirmwareId,
  postFirmware,
  PutFirmwareId,
  firmwareVersions
} from '@/base/api/FirmwareApi'
import {getDeviceModel, getDeviceModelId} from '@/base/api/DeviceModelApi'
import {getDeviceSort} from '@/base/api/DeviceSortApi'

export class FirmwareBloc {

  page: Page<DeviceSort> = new Page<DeviceSort>([], 0, 1, 15, 0, []);
  page1: Page<DeviceModel> = new Page<DeviceModel>([], 0, 1, 15, 0, []);
  firmware: Page<Firmware> = new Page<Firmware>([], 0, 1, 10, 0, []);
  model: any = {
    agentId: 0,
    city: '',
    cityId: '',
    deviceSn: '',
    url: '',
    extName: '',
    modelId: 0,
    province: '',
    provinceId: '',
    sortId: 0,
    issueStatus: '',
    accountName: '',
    versions: 0 || undefined
  };
  firmwareId = new Firmware()
  sortList: any = [];
  modelList: any = [];
  versions: any = []

  loading = false;

  getAllSort(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    return getDeviceSort({
      pageIndex,
      pageSize,
      sortDirection
    }).then((res) => {
      deepAssign(this.page, res.data);
      this.sortList = this.page.items.map(function (item) {
        var obj = {id: 0, sortName: ''};
        // @ts-ignore
        obj.id = item.id;
        obj.sortName = item.sortName;
        return obj
      });
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  getAllModel(pageIndex: number, pageSize: number, sortId: number, sortDirection: string, companyId) {
    this.loading = true;
    return getDeviceModel({
      pageIndex,
      pageSize,
      sortId,
      sortDirection,
      companyId,
      id: this.model.id,
      modelName: this.model.modeName
    }).then((res) => {
      deepAssign(this.page1, res.data);
      this.modelList = this.page1.items.map(function (item) {
        var obj = {id: 0, modelName: ''};
        obj.id = item.id;
        obj.modelName = item.modelName;
        return obj
      });
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  getModel(id: number) {
    this.loading = true;
    return getDeviceModelId({id}).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  addFirmware(
    describe: string,
    modelId: number,
    sortId: number,
    url: string,
    versions: number,
    issueStatus: string) {
    this.loading = true;
    return postFirmware({
      describe, extName: this.model.extName, modelId, sortId, url, versions, issueStatus
    }).then((res) => {
      this.getFirmware(this.firmware.pageIndex, 10, 'DESC', this.model)
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  getFirmware(
    pageIndex: number, pageSize: number, sortDirection: string, model: any) {
    this.loading = true;
    return getFirmware({
      pageIndex: pageIndex,
      pageSize: pageSize,
      sortDirection: sortDirection,
      accountName: model.accountName,
      versions: model.versions,
      issueStatus: model.issueStatus,
      modelId: model.modelId
    }).then((res) => {
      deepAssign(this.firmware, res.data);
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  getVersions() {
    this.loading = true;
    return firmwareVersions({modelId: this.model.modelId}).then((res) => {
      this.versions = res.data
    }).finally(() => {
      this.loading = false;
    })
  }

  toModifyItem(id: number) {
    this.loading = true;
    return getFirmwareId(
      id
    ).then((res) => {
      deepAssign(this.firmwareId, res.data);
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  modifyItem(describe: string,
             id: number,
             issueStatus: string,
             modelId: number,
             sortId: number,
             url: string,
  ) {
    this.loading = true;
    return PutFirmwareId({
      describe,
      extName: this.firmwareId.extName,
      id,
      issueStatus,
      modelId,
      sortId,
      url
    }).then((res) => {
      deepAssign(this.firmwareId, res.data);
      this.getFirmware(this.firmware.pageIndex, 10, 'DESC', this.model)
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  removeItem(dateItem: Firmware) {
    this.loading = true;
    return deleteFirmwareId(dateItem.id)
      .then((res) => {
        this.getFirmware(this.firmware.pageIndex, 10, 'DESC', this.model)
        return res;
      })
      .finally(() => {
        this.loading = false;
      })
  }

}
